var util = require('../../../utils/util.js');
var api = require('../../../config/api.js');

var app = getApp();

Page({
  data: {
    type: 0,
    shoucanglist: [],
    page: 1,
    size: 10,
    totalPages: 1,
    memberUserid:''
  },
  

  getCollectList: function () {
    let that = this;
    util.request(api.ShoucangList, {
      page: that.data.page,
      size: that.data.size
    }).then(function (res) {
      if (res.result == 1) {
        if (res.data.result.length > 0) {
          that.setData({
            shoucanglist: that.data.shoucanglist.concat(res.data.result),
            page: that.data.page + 1
          });
        } else {
          wx.showToast({
            title: '已经被您看完了!',
            icon: 'none',
            duration: 2000
          });
          return false;
        }
      }
    });
  },
  onLoad: function(options) {
  
  },
  onReachBottom() {
    this.getCollectList();
  },
  onReady: function() {

  },
  onShow: function() {
    let that=this;
    if (that.data.shoucanglist.length == 0) {
    this.getCollectList();
    }
  },
  onHide: function() {
    // 页面隐藏

  },
  onUnload: function() {
    // 页面关闭
  },
  openGoods(event) {
    let that = this;
    let goodsid = event.currentTarget.dataset.goodsid;
    //触摸时间距离页面打开的毫秒数  
    var touchTime = that.data.touchEnd - that.data.touchStart;
    console.log(touchTime);
    //如果按下时间大于350为长按  
    if (touchTime > 350) {
      wx.showModal({
        title: '',
        content: '确定删除吗？',
        success: function(res) {
          if (res.confirm) {
            util.request(api.ShouCangAddOrDelete, {
              type: 1,
              goodsid: goodsid
            }, 'POST').then(function(res) {
              if (res.result == 1) {
                wx.showToast({
                  title: '删除成功',
                  icon: 'success',
                  duration: 2000
                });
                let _shoucanglist = that.data.shoucanglist;
                for (let i = 0; i < _shoucanglist.length; i++) {
                  if (_shoucanglist[i].goods.id == goodsid) {
                    _shoucanglist.splice(i, 1)
                  } 
                }
                that.setData({
                  shoucanglist: _shoucanglist
                });
              }
            });
          }
        }
      })
    } else {
      wx.navigateTo({
        url: '/pages/goods/goods?id=' + goodsid,
      });
    }
  },
})